#include <iostream>
#include <stdio.h>
#include <algorithm>
#include <string>

using namespace std;

int n;
char ss[20], out[20];
int tt[20], len, wide;
int flag[20];
char order[]={"AaBbCcDdEeFfGgHhIiJjKkLlMmNnOoPpQqRrSsTtUuVvWwXxYyZz"};
int orderlen;

void print()
{
    int i;
    for (i=1; i<=len; ++i)
    	printf("%c", out[tt[i]]);
   	printf("\n");
}    

void di(int pt)
{
    int i;
    if (pt>len)
    {
        print();
    }
    else
    {
        for (i=1; i<=out[0]; ++i)
        	if (flag[i])
       		{
       		    tt[pt] = i;
       		    --flag[i];
       		    di(pt+1);
       		    ++flag[i];
       		}    
    }        
}    
int comp(char t1, char t2)
{
	int s1,s2;
	for (s1=0; s1<orderlen; ++s1)
		if (order[s1]==t1) break;
	for (s2=0; s2<orderlen; ++s2)
		if (order[s2]==t2) break;
	return s2>s1;
  
}    
int main()
{
	int i, j;
	char ch;
 	
 	orderlen = strlen(order);
 	scanf("%d", &n);
    while (n-->0)
    {
        memset(ss, 0, sizeof(ss));
        scanf("%s", ss);
        len = strlen(ss);
     	sort(ss,ss+len,comp);
 //     	printf("sort : %s\n", ss);
	    memset(flag, 0, sizeof(flag));
	    memset(out, 0, sizeof(out));

	    out[++out[0]] = ss[0];	    
	    flag[1] = 1;
	    for (i=1; i<len; ++i)
	    	if (ss[i] == ss[i-1])
	    	{
	    	    flag[out[0]]++;
	    	}    
	    	else
	    	{
	    	    out[++out[0]] = ss[i];
	    	    flag[out[0]] = 1;
	    	}    
        
        di(1);        
    }        
    
    
    
    return 0;
}    